# Contributor: rubicon <rubicon@mailo.com>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=ocp-index
pkgver=1.3.6
pkgrel=0
pkgdesc="Lightweight completion and documentation browsing for OCaml libraries"
url="http://www.typerex.org/ocp-index.html"
arch="all !riscv64 !loongarch64" # ocaml
license="LGPL-2.1-only WITH OCaml-LGPL-linking-exception AND GPL-3.0-only"
depends_dev="
	ocaml-cmdliner-dev
	ocaml-compiler-libs
	ocaml-ocp-indent-dev
	ocaml-re-dev
	"
makedepends="$depends_dev dune ocaml ocaml-cppo"
subpackages="
	$pkgname-doc
	$pkgname-emacs::noarch
	$pkgname-vim::noarch
	ocaml-$pkgname-dev:_libdev
	ocaml-$pkgname:_lib
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/OCamlPro/ocp-index/archive/refs/tags/$pkgver.tar.gz"
_ocamldir=usr/lib/ocaml

# 32-bit archs
case "$CARCH" in
	arm*|x86) options="$options textrels" ;;
esac

build() {
	dune build -p ocp-index --verbose
}

check() {
	dune runtest --build-dir=.testenv -p ocp-index
}

package() {
	dune install \
		--destdir="$pkgdir" \
		ocp-index
}

emacs() {
	pkgdesc="$pkgdesc in Emacs"
	depends="$pkgname=$pkgver-r$pkgrel"
	install_if="$pkgname=$pkgver-r$pkgrel emacs"

	amove usr/share/emacs
}

vim() {
	pkgdesc="$pkgdesc in vim"
	depends="$pkgname=$pkgver-r$pkgrel"
	install_if="$pkgname=$pkgver-r$pkgrel vim"

	amove usr/share/ocp-index/vim
}

_libdev() {
	pkgdesc="$pkgdesc (development files)"
	depends="$depends_dev ocaml-$pkgname=$pkgver-r$pkgrel"

	cd "$pkgdir"

	local path; find $_ocamldir \( \
			-name '*.cmt' -o \
			-name '*.cmti' -o \
			-name '*.ml' -o \
			-name '*.mli' \
		\) | while read -r path
	do
		amove "$path"
	done

}

_lib() {
	pkgdesc="$pkgdesc (library)"

	amove usr/lib/ocaml
}

prepare_py_provides() {
	local datadir="${subpkgdir:-$pkgdir}"
	local pkgbasedir=${pkgbasedir:-"$startdir/pkg"}
	local controldir="$pkgbasedir"/.control.${subpkgname:-$pkgname}

	options_has "!tracedeps" && return 0

	ocaml4-abuild-find provides \
		"$datadir"/usr/lib/ocaml \
		"$controldir" \
		"$pkgver-r$pkgrel"
}

scan_python3_dependency() {
	local controldir="$2" datadir="$3"

	ocaml4-abuild-find requires \
		"$datadir"/usr/lib/ocaml \
		"$controldir"
}

sha512sums="
f8f97e846e37473a77636518ceefafa72a62f26ed9e7fc65ee4625498bcc1797187e895d903dfb2e874e93889956ee02b2e1f6f90c61717107db8e96d388d65a  ocp-index-1.3.6.tar.gz
"
